/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.isfd.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.core.utils.MapstructUtils;
import com.comac.ins.common.excel.utils.ExcelUtil;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.isfd.constant.enums.IsfdMetadataTableNameEnum;
import com.comac.ins.isfd.domain.IsfdCompositeMaterial;
import com.comac.ins.isfd.domain.IsfdLayupMetalCaseMeta;
import com.comac.ins.isfd.domain.bo.IsfdLayupMetalCaseMetaBo;
import com.comac.ins.isfd.domain.bo.query.IsfdLayupMetalCaseMetaQueryBo;
import com.comac.ins.isfd.domain.vo.IsfdLayupMetalCaseMetaVo;
import com.comac.ins.isfd.mapper.IsfdLayupMetalCaseMetaMapper;
import com.comac.ins.isfd.service.IIsfdLayupMetalCaseMetaService;
import com.comac.ins.isfd.service.IIsfdMetadataImportService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author Lion Li
 * @date 2024-08-02
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class IsfdLayupMetalCaseMetaServiceImpl implements IIsfdLayupMetalCaseMetaService, IIsfdMetadataImportService {

    @Autowired
    private final IsfdLayupMetalCaseMetaMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 主键
     * @return 【请填写功能名称】
     */
    @Override
    public IsfdLayupMetalCaseMetaVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询【请填写功能名称】列表
     *
     * @param bo        查询条件
     * @return 【请填写功能名称】分页列表
     */
    @Override
    public TableDataInfo<IsfdLayupMetalCaseMetaVo> queryPageList(IsfdLayupMetalCaseMetaQueryBo bo) {
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(bo.getPageNum());
        pageQuery.setPageSize(bo.getPageSize());
        LambdaQueryWrapper<IsfdLayupMetalCaseMeta> lqw = buildQueryWrapper(bo);
        Page<IsfdLayupMetalCaseMetaVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的【请填写功能名称】列表
     *
     * @param bo 查询条件
     * @return 【请填写功能名称】列表
     */
    @Override
    public List<IsfdLayupMetalCaseMetaVo> queryList(IsfdLayupMetalCaseMetaQueryBo bo) {
        LambdaQueryWrapper<IsfdLayupMetalCaseMeta> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IsfdLayupMetalCaseMeta> buildQueryWrapper(IsfdLayupMetalCaseMetaQueryBo bo) {
        LambdaQueryWrapper<IsfdLayupMetalCaseMeta> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getAttributeCode()), IsfdLayupMetalCaseMeta::getAttributeCode, bo.getAttributeCode());
        lqw.eq(bo.getExx() != null, IsfdLayupMetalCaseMeta::getExx, bo.getExx());
        lqw.eq(bo.getEyy() != null, IsfdLayupMetalCaseMeta::getEyy, bo.getEyy());
        lqw.eq(bo.getGxy() != null, IsfdLayupMetalCaseMeta::getGxy, bo.getGxy());
        lqw.eq(bo.getThickness() != null, IsfdLayupMetalCaseMeta::getThickness, bo.getThickness());
        lqw.eq(StringUtils.isNotBlank(bo.getPshellNumber()), IsfdLayupMetalCaseMeta::getPshellNumber, bo.getPshellNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialCode()), IsfdLayupMetalCaseMeta::getMaterialCode, bo.getMaterialCode());
        lqw.in(CollectionUtil.isNotEmpty(bo.getMetadataManagementIds()), IsfdLayupMetalCaseMeta::getMetadataManagementId, bo.getMetadataManagementIds());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param bo 【请填写功能名称】
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(IsfdLayupMetalCaseMetaBo bo) {
        IsfdLayupMetalCaseMeta add = MapstructUtils.convert(bo, IsfdLayupMetalCaseMeta.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param bo 【请填写功能名称】
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(IsfdLayupMetalCaseMetaBo bo) {
        IsfdLayupMetalCaseMeta update = MapstructUtils.convert(bo, IsfdLayupMetalCaseMeta.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IsfdLayupMetalCaseMeta entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除【请填写功能名称】信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public IsfdLayupMetalCaseMetaVo queryByLayCode(String layCode) {
        return baseMapper.selectVoOne(new LambdaQueryWrapper<IsfdLayupMetalCaseMeta>().eq(IsfdLayupMetalCaseMeta::getAttributeCode, layCode));
    }

    @Override
    public String selectByTableName() {
        return IsfdMetadataTableNameEnum.ISFD_LAYUP_METAL_CASE_META.getValue();
    }

    @Override
    public Long excelImport(File excelImportFile, String metadataManagementId) {
        long successCount = 0L;
        try (InputStream is = new FileInputStream(excelImportFile)) {
            List<IsfdLayupMetalCaseMetaVo> targetVo = ExcelUtil.importExcel(is, IsfdLayupMetalCaseMetaVo.class);
            for (IsfdLayupMetalCaseMetaVo vo : targetVo) {
                IsfdLayupMetalCaseMetaBo bo = MapstructUtils.convert(vo, IsfdLayupMetalCaseMetaBo.class);

                try{
                    bo.setMetadataManagementId(metadataManagementId);
                    insertByBo(bo);
                    successCount++;
                } catch (Exception e) {
                    log.error("金属壳单元属性数据导入失败", e);
                }
            }

        } catch (Exception e) {
            log.error("sheet文件转换为实体类失败", e);
            throw new BaseException("sheet文件转换为实体类失败");
        }

        return successCount;
    }
}
